﻿using System;
using System.Collections.Generic;
using System.Linq;
using WholesaleOnline.Contract;
using WholesaleOnline.DataContract.Extension;
using WholesaleOnline.DataContract.Messaging;
using WholesaleOnline.Model;
using WholesaleOnline.Model.Repository;
using WholesaleOnline.Service.Implementations;

namespace WholesaleOnline.Service.Services
{
    public class BuyerHomePageService : IBuyerHomePageService
    {
        private IBuyerRepository m_buyerRepository = null;
        private ISellerRepository m_sellerReposiotry = null;

        public BuyerHomePageService(IBuyerRepository buyerRepository, ISellerRepository sellerRepository)
        {
            m_buyerRepository = buyerRepository;
            m_sellerReposiotry = sellerRepository;
        }

        public GetBuyerResponse GetBuyer(GetBuyerRequest request)
        {
            GetBuyerResponse response = new GetBuyerResponse();
            try
            {
                IEnumerable<WoBuyer> buyers = m_buyerRepository.FindBy(HomePageServiceQueryGenerator.BuildQueryBy(request));
                if (0 < buyers.Count())
                {
                    response.BuyerName = buyers.First().Name;
                    response.Success = true;
                }
            }
            catch (Exception)
            {
                response.Message = "该买家信息不存在!";
            }
            return response;
        }

        public GetAllSellersResponse GetAllSellers()
        {
            GetAllSellersResponse response = new GetAllSellersResponse();
            try
            {
                response.Sellers = m_sellerReposiotry.FindAll().OrderBy(s => s.SortOrder).ConvertToSellerView();
                response.Success = true;
            }
            catch (Exception)
            {
                response.Message = "获取商家列表失败!";
            }
            return response;
        }
    }
}
